<?php

namespace App\Models;

use App\admin\Service\OA;
use App\Jobs\Fun;
use App\Services\OrdersPayService;
use App\Models\Admin\UserModel;

class UploadPicModel extends Model {

    /**
     * 记录用户上传的图片,避免重复上传   path+md5+(big/small)+ext=图片访问地址
     * 表示唯一图片标识符,加上后缀后表示大图和小图
    比如
    表示小图 md5small.png
    表示小图  md5big.png
    */
    protected $table = 'uploadpic';
    const TypeBig='big';
    const TypeSmall='small';
    public $pic;
    public function attributeLabels() {
        return [
            'md5' => 'md5',//
            'path' => '图片路径',
            'time' => '创建时间',
            'ext' => '类型',
            'pic' => '图片',
        ];
    }


    /**
     * 添加图片路径
    */
    public static function addPic($md5,$folderName,$extension){
        $new=new self;
        $new->md5=$md5;
        $new->path=$folderName;
        $new->ext=$extension;
        $new->time=time();
        $new->save();
    }

    /**
     * 查找指定的图片
     * path+md5+(big/small)+ext=图片访问地址
    */
    public static function getFilePath($md5){
        $first= self::where([
            'md5'=>$md5
        ])->select([
            'path','ext'
        ])->first();
        if($first){
            return [
                self::TypeSmall=>$first['path'].$md5.self::TypeSmall.'.'.$first['ext'],
                self::TypeBig=>$first['path'].$md5.self::TypeBig.'.'.$first['ext']
            ];
        }else{
            return false;
        }
    }

}
